<template>
  <card>
    <p slot="title">
        <Icon type="compose"></Icon>
        基本信息
    </p>
    <Form ref="formItem" :model="formItem" :rules="ruleForm" :label-width="120">
        <Row type="flex">
            <Col span="6">
                <FormItem label="商品：" prop="itemId">
                    <Select v-model="formItem.itemId" size="large" style="width:100%" :label-in-value=true @on-change="changeItem">
                        <Option v-for="item in products" :value="item.id" :key="item.id">{{ item.name }}</Option>
                    </Select>
                </FormItem>
            </Col>
            <Col span="6">
                <FormItem label="警戒值：" prop="quantity">
                    <Input v-model="formItem.quantity" size="large" placeholder="请填写内容..."></Input>
                </FormItem>
            </Col>
            <Col span="6">
                <FormItem label="开启状态：" prop="status">
                    <i-Switch v-model="formItem.status" size="large" :true-value="1" :false-value="0">
                        <span slot="open">开启</span>
                        <span slot="close">关闭</span>
                    </i-Switch>
                </FormItem>
            </Col>
        </Row>
        <Row type="flex">
            <Col span="24">
                <FormItem>
                    <Button @click="save" size="large" type="primary">保存</Button>
                    <Button @click="close" size="large" type="ghost" style="margin-left: 8px">关闭</Button>
                </FormItem>
            </Col>
        </Row>
    </Form>
  </card>
</template>

<script>
export default {
    data () {
        return {
            baseURL: '/user/inventory/warning',
            productURL: '/user/product',
            products: [],
            formItem: {
                id: '',
                itemId: '',
                itemName: '',
                quantity: '',
                status: ''
            },
            ruleForm: {
                itemId: [
                    { required: true, message: '请选择商品', pattern: /.+/, trigger: 'change' }
                ],
                quantity: [
                    { required: true, message: '请填写警戒值', pattern: /.+/, trigger: 'change' }
                ],
                status: [
                    { required: true, message: '请选择是否开启', pattern: /.+/, trigger: 'change' }
                ]
            }
        }
    },
    methods: {
        init () {
            // 加载商品
            this.loadProduct()
            // 表单初始化
            this.$bus.fillData(this.formItem, this.$route.query.row)
        },
        loadProduct () {
            let url = this.productURL + '/findAll'
            this.$ajax.get(url).then((response) => {
                let {code, data, msg} = response.data
                if (code === 0) {
                    this.products = data
                } else {
                    this.$bus.alert('error', msg)
                }
            })
        },
        changeItem (option) {
            this.formItem.itemId = option.value
            this.formItem.itemName = option.label
        },
        save () {
            this.$refs['formItem'].validate((valid) => {
                if (valid) {
                    let url = this.baseURL + '/saveOrUpdate'
                    this.$ajax.post(url, this.formItem).then((response) => {
                        let {code, msg} = response.data
                        if (code === 0) {
                            this.$bus.alert('success', msg)
                            this.$bus.init(this.formItem)
                        } else {
                            this.$bus.alert('error', msg)
                        }
                    })
                }
            })
        },
        close () {

        }
    },
    beforeRouteEnter (to, from, next) {
        next(vm => {
            vm.init()
        })
    }
}
</script>

<style>

</style>
